package com.sec.android.app.myfiles.util;

import android.content.Context;

import com.sec.android.app.myfiles.MyFilesTestRunner;
import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.trash.TrashFileListFragment;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.mockclass.FileMock;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.shadow.custom.ShadowCloudGatewaySignInUtils;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.reflect.Whitebox;
import org.robolectric.RuntimeEnvironment;

@RunWith(MyFilesTestRunner.class)
public class testUiUtils {
    NavigationInfo mNaviInfo;
    Context mContext;

    static final String TEST_PATH = "/testRoot/testDir";

    @Before
    public void setUp() throws Exception {
        FileRecord record = FileRecord.createFileRecord(FileRecord.StorageType.Local, TEST_PATH);
        mNaviInfo = NavigationInfo.getInstance(NavigationInfo.NavigationMode.Normal, record);

        mContext = RuntimeEnvironment.application;
        ShadowCloudGatewaySignInUtils.init();
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testGetInternalStorageRootResId() {
        int resId = UiUtils.getInternalStorageRootResId();
        Assert.assertEquals(R.string.my_device, resId);
    }

    @Test
    public void testIsCheckableEnabled() {
        // Picker mode check
        mNaviInfo.setNavigationMode(NavigationInfo.NavigationMode.Pick_files);
        FileRecord record = FileRecord.createFileRecord(FileRecord.StorageType.Local, TEST_PATH);
        Assert.assertTrue(UiUtils.needCheckableCheck(mNaviInfo, record, false));

        mNaviInfo.setNavigationMode(NavigationInfo.NavigationMode.Pick_one_file);
        record = FileRecord.createFileRecord(FileRecord.StorageType.Local, TEST_PATH);
        Assert.assertTrue(UiUtils.needCheckableCheck(mNaviInfo, record, false));

        // Trash case check
        mNaviInfo.setNavigationMode(NavigationInfo.NavigationMode.Normal);
        record = FileRecord.createFileRecord(FileRecord.StorageType.Trash, TEST_PATH);
        AbsMyFilesFragment fragment = new TrashFileListFragment();
        fragment.setActionModeType(AbsMyFilesFragment.ActionModeType.SELECT_FILE_FOLDER_OPERATION);
        mNaviInfo.setCurFragment(fragment);
        Assert.assertTrue(UiUtils.needCheckableCheck(mNaviInfo, record, false));

        fragment.setActionModeType(AbsMyFilesFragment.ActionModeType.EDIT_FILE_FOLDER_OPERATION);
        mNaviInfo.setCurFragment(fragment);
        Assert.assertTrue(UiUtils.needCheckableCheck(mNaviInfo, record, false));

        fragment.setActionModeType(AbsMyFilesFragment.ActionModeType.SELECT_SHARE_FILE_OPERATION);
        mNaviInfo.setCurFragment(fragment);
        Assert.assertTrue(UiUtils.needCheckableCheck(mNaviInfo, record, false));
    }

    @Test
    public void testIsCheckableDisabled() {
        // record storage check
        mNaviInfo.setNavigationMode(NavigationInfo.NavigationMode.Normal);
        FileRecord record = FileRecord.createFileRecord(FileRecord.StorageType.Local, TEST_PATH);
        Assert.assertFalse(UiUtils.needCheckableCheck(mNaviInfo, record, false));

        // Action mode check
        record = FileRecord.createFileRecord(FileRecord.StorageType.Trash, TEST_PATH);
        AbsMyFilesFragment fragment = new TrashFileListFragment();
        fragment.setActionModeType(AbsMyFilesFragment.ActionModeType.NORMAL);
        mNaviInfo.setCurFragment(fragment);
        Assert.assertFalse(UiUtils.needCheckableCheck(mNaviInfo, record, false));
    }

    @Test
    public void testIsInRTLMode() {
        Assert.assertFalse(UiUtils.isInRTLMode(mContext));
    }


    @Test
    public void testIsValidClick() {
        Whitebox.setInternalState(UiUtils.class, "mLastClickTime", 0L);
        Whitebox.setInternalState(UiUtils.class, "sPrevId", 0);

        Assert.assertTrue(UiUtils.isValidClick(0));
        Assert.assertFalse(UiUtils.isValidClick(0));

        long curTime = System.currentTimeMillis();
        long delta = (long) Whitebox.getInternalState(UiUtils.class, "DOUBLE_CLICK_TIME_DELTA");
        long prevTime = curTime - delta;
        Whitebox.setInternalState(UiUtils.class, "mLastClickTime", prevTime);
        Assert.assertTrue(UiUtils.isValidClick(0));

        Assert.assertTrue(UiUtils.isValidClick(1));

    }


    @Test
    public void testIsCloudGatewayEnabled() {
        Assert.assertFalse(UiUtils.isCloudGatewayEnabled(mContext));

        ShadowCloudGatewaySignInUtils.isPlatformEnabled = true;
        Assert.assertTrue(UiUtils.isCloudGatewayEnabled(mContext));
    }

    @Test
    public void testIsCheckable() {
        //test null record
        Assert.assertFalse(UiUtils.isCheckable(mContext, null, mNaviInfo, true));
        //test directory
        FileRecord record = FileRecord.createFileRecord(FileRecord.StorageType.Local, TEST_PATH);
        Assert.assertFalse(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));
        //test play ready type
        record = FileRecord.createFileRecord(FileRecord.StorageType.Local, 0, TEST_PATH, "name.wmv",
                1000L, 0L, FileType.WMV, 0, 0, 0);
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));
        //Test DRM file type
        record = FileRecord.createFileRecord(FileRecord.StorageType.Local, 0, TEST_PATH, "name.odf",
                1000L, 0L, FileType.ODF, 0, 0, 0);
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));
        //test 0 size file
        record = FileRecord.createFileRecord(FileRecord.StorageType.Local, 0, TEST_PATH, "name.odf",
                0L, 0L, FileType.ODF, 0, 0, 0);
        long oldSize = FileMock.FILE_MOCK_SIZE;
        FileMock.FILE_MOCK_SIZE = 0L;
        Assert.assertFalse(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));
        FileMock.FILE_MOCK_SIZE = oldSize;
        //Test downloads play ready
        record = FileRecord.createFileRecord(FileRecord.StorageType.Downloads, 0, TEST_PATH, "name.wmv",
                1000L, 0L, FileType.WMV, 0, 0, 0);
        record.setFileType(FileType.WMV);
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));
        //Test downloads drm file
        record = FileRecord.createFileRecord(FileRecord.StorageType.Downloads, 0, TEST_PATH, "name.odf",
                1000L, 0L, FileType.ODF, 0, 0, 0);
        record.setFileType(FileType.ODF);
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));
        //Test downloads other
        record = FileRecord.createFileRecord(FileRecord.StorageType.Downloads, 0, TEST_PATH, "name.jpeg",
                1000L, 0L, FileType.JPEG, 0, 0, 0);
        record.setFileType(FileType.JPEG);
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));
        //Test trash
        record = FileRecord.createFileRecord(FileRecord.StorageType.Trash, 0, TEST_PATH, "name.jpeg",
                1000L, 0L, FileType.JPEG, 0, 0, 0);
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));
        //Test other
        record = FileRecord.createFileRecord(FileRecord.StorageType.Search, 0, TEST_PATH, "name.jpeg",
                1000L, 0L, FileType.JPEG, 0, 0, 0);
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, true));
        Assert.assertTrue(UiUtils.isCheckable(mContext, record, mNaviInfo, false));

    }
}